import 'package:dio/dio.dart';
import 'package:flutter_play_android/common/common.dart';
import 'package:flutter_play_android/data/model/user_model.dart';
import 'package:flutter_play_android/utils/sp_util.dart';

class User {
  static final User singleton = User._internal();

  User._internal();

  factory User() {
    return singleton;
  }

  List<String>? cookie;
  String? userName;

  void saveUserInfo(UserModel _userModel, Response response) {
    List<String>? cookies = response.headers["set-cookie"];
    cookie = cookies;
    userName = _userModel.data?.username;
    saveInfo();
  }

  Future<void> getUserInfo() async {
    List<String> cookies = SPUtil.getStringList(Constants.COOKIES_KEY);
    if (cookies.isNotEmpty) cookie = cookies;
    String username = SPUtil.getString(Constants.USERNAME_KEY);
    if(username.isNotEmpty) userName = username;
  }

  saveInfo() async{
    SPUtil.putStringList(Constants.COOKIES_KEY, cookie ?? List.empty());
    SPUtil.putString(Constants.USERNAME_KEY, userName ?? '');
  }

  void clearUserInfo(){
    cookie = null;
    userName = null;
    SPUtil.remove(Constants.COOKIES_KEY);
    SPUtil.remove(Constants.USERNAME_KEY);
  }

}
